package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.Blog;
import dao.BlogDao;
import dao.DButi;
import dao.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-11-08
 * Time: 18:44
 */
@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    ObjectMapper mapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null){
            resp.sendRedirect("login.html");
            return;
        }
        String blogId = req.getParameter("blogId");
        if(blogId == null){
            BlogDao blogDao = new BlogDao();
            try {
                List<Blog> blogs = blogDao.getblogs();
                if(blogs == null){
                    blogs = new ArrayList<>();
                }
                String json = mapper.writeValueAsString(blogs);
                resp.setContentType("application/json; charset=utf8");
                resp.getWriter().write(json);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else{
            BlogDao blogDao = new BlogDao();
            try {
                Blog blog = blogDao.getblog(Integer.parseInt(blogId));
                if(blog == null){
                    blog = new Blog();
                }
                String json = mapper.writeValueAsString(blog);
                resp.setContentType("application/json; charset=utf8");
                resp.getWriter().write(json);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if(session == null){
            resp.sendRedirect("login.html");
            return;
        }
        User user = (User) session.getAttribute("user");
        if(user == null){
            resp.sendRedirect("login.html");
            return;//
        }
        req.setCharacterEncoding("utf8");//
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if(title == null || content == null || "".equals(title) || "".equals(content)){
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("标题或者正文为空");
            return;
        }
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserId());
        BlogDao blogDao = new BlogDao();
        try {
            blogDao.insert(blog);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        resp.sendRedirect("blog_list.html");
    }
}
